/**
 * @file   testStringUtil.cpp
 * @author wolfhead <wolfhead@wolfhead-laptop>
 * @date   Thu Aug 18 13:36:11 2011
 * 
 * @brief  
 * 
 * 
 */
#include "gtest/gtest.h"
#include <acapella/string/string_util.hpp>

TEST(StringUtilTest, ExplodeTest)
{
    int i = 1000000;
    while (i--)
    {
        std::vector<std::string> result = 
            acapella::StringUtil::Explode(
                "1,2:3.45,6,7", 
                ",:.");
    }

}

TEST(StringUtilTest, ExplodeWithNoDelimiterTest)
{
    std::vector<std::string> result = acapella::StringUtil::Explode("123456789", "sdajkldhferqpeiwfaldf");

    EXPECT_EQ("123456789", result[0]);
}

TEST(StringUtilTest, ExplodeWithAllDelimiterTest)
{
    std::vector<std::string> result = acapella::StringUtil::Explode("sdasdasda", "sdajkldhferqpeiwfaldf");

    EXPECT_EQ((unsigned int)10, result.size());
}

TEST(StringUtilTest, ImplodeTest)
{
    std::vector<std::string> test;
    test.push_back("1");
    test.push_back("2");
    test.push_back("3");

    EXPECT_EQ("1,2,3", acapella::StringUtil::Implode(test, ","));
}


TEST(StringUtilTest, ToValueTest)
{
    using namespace acapella;

    int i = StringUtil::ToValue<int>("123");
    double d = StringUtil::ToValue<double>("123.123");
    
    EXPECT_EQ(123, i);
    EXPECT_EQ(123.123, d);
}

TEST(StringUtilTest, ToStringTest)
{
    using namespace acapella;

    std::string str;
    str = StringUtil::ToString(123);
    EXPECT_EQ("123", str);

    str = StringUtil::ToString(123.123);
    EXPECT_EQ("123.123", str);
}

TEST(StringUtilTest, EscapeTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Escape("1234567890\r\n\b\a123");

    EXPECT_EQ("1234567890\\r\\n\\b\\a123", str);
}

TEST(StringUtilTest, UnescapeTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Unescape("1234567890\\r\\n\\b\\a123");

    EXPECT_EQ("1234567890\r\n\b\a123", str);
}

TEST(StringUtilTest, TrimAllTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Trim("1234567890123", "1234567890");

    EXPECT_EQ("", str);
}

TEST(StringUtilTest, TrimEmptyTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Trim("", "1234567890");

    EXPECT_EQ("", str);
}

TEST(StringUtilTest, TrimTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Trim("1234567890123", "123");

    EXPECT_EQ("4567890", str);
}


TEST(StringUtilTest, TrimLeftTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::TrimLeft("1234567890123", "123");

    EXPECT_EQ("4567890123", str);
}

TEST(StringUtilTest, TrimRightTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::TrimRight("1234567890123", "123");

    EXPECT_EQ("1234567890", str);
}


TEST(StringUtilTest, FormatTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::Format<256>("%d %s", 123, "123");

    EXPECT_EQ("123 123", str);
}


TEST(StringUtilTest, ReplaceAllTest)
{
    using namespace acapella;

    std::string str;

    str = StringUtil::ReplaceAll("121121", "12", "3");

    EXPECT_EQ("3131", str);
}


TEST(StringUtilTest, Bin2HexTest)
{
    using namespace acapella;
    unsigned char high;
    unsigned char low;

    StringUtil::Bin2Hex(0xFF, high, low);
    EXPECT_EQ('F', high);
    EXPECT_EQ('F', low);


    StringUtil::Bin2Hex(0x10, high, low);
    EXPECT_EQ('1', high);
    EXPECT_EQ('0', low);  

    StringUtil::Bin2Hex(0x00, high, low);
    EXPECT_EQ('0', high);
    EXPECT_EQ('0', low);  

    StringUtil::Bin2Hex(0xA9, high, low);
    EXPECT_EQ('A', high);
    EXPECT_EQ('9', low); 
}

TEST(StringUtilTest, Hex2BinTest)
{
    using namespace acapella;

    unsigned char bin;

    StringUtil::Hex2Bin('F', 'F', bin);
    EXPECT_EQ(0xFF, bin);

    StringUtil::Hex2Bin('0', '0', bin);
    EXPECT_EQ(0x00, bin);
    
    StringUtil::Hex2Bin('1', 'A', bin);
    EXPECT_EQ(0x1A, bin);
}


TEST(StringUtilTest, Bin2HexTest2)
{
    using namespace acapella;

    const unsigned char binary[] = {0x11, 0x00, 0xFF, 0x97};

    std::string strBin((const char *)binary, 4);
    
    std::string strHex = StringUtil::Bin2Hex(strBin.data(), strBin.size());

    EXPECT_EQ("1100FF97", strHex);
}

TEST(StringUtilTest, Hex2BinTest2)
{
    using namespace acapella;

    const unsigned char binary[] = {0x11, 0x00, 0xFF, 0x97};
    
    std::string strBin = StringUtil::Hex2Bin("1100FF97");

    EXPECT_EQ(0, memcmp(strBin.data(), binary, 4));
}


TEST(StringUtilTest, ToUpperTest)
{
    using namespace acapella;

    std::string str = StringUtil::ToUpper("abcxyzHHH");
    
    EXPECT_EQ("ABCXYZHHH", str);
}

TEST(StringUtilTest, ToLowerTest)
{
    using namespace acapella;

    std::string str = StringUtil::ToLower("ABCXYZhhh");
    
    EXPECT_EQ("abcxyzhhh", str);
}

TEST(StringUtilTest, ParseParamTest)
{
    using namespace acapella;

    std::map<std::string, std::string> param =
        StringUtil::ParseParam("1=2;3:4,5=6,7", "=:", ",;");

    EXPECT_EQ("2", param["1"]);
    EXPECT_EQ("4", param["3"]);  
    EXPECT_EQ("6", param["5"]);  
}

TEST(StringUtilTest, BuildParamTest)
{
    using namespace acapella;

    std::map<std::string, std::string> param;
    param["5"] = "1";
    param["6"] = "2";

    std::string str = StringUtil::BuildParam(param, ":", ";");

    EXPECT_EQ("5:1;6:2", str);
}

TEST(StringUtilTest, QuoteTest)
{
    using namespace acapella;
    std::string test = StringUtil::Quote("123");

    EXPECT_EQ("\"123\"", test);
}
